import model
import pandas as pd
import pickle
from sklearn.model_selection import cross_val_predict,KFold,cross_validate
from sklearn.metrics import mean_absolute_error,r2_score
from sklearn.utils import shuffle

from sklearn.ensemble import RandomForestRegressor
from lightgbm import LGBMRegressor


def run_predict(df_predict, category_fields, numeric_fields, onehot_encoder, *regressors):
    x_pred = model.convert_testdf_to_matrix(df_predict[category_fields + numeric_fields], category_fields ,numeric_fields, onehot_encoder)

    df_predict.loc[:, 'predict'] = 0
    for regressor in regressors:
        y_pred = regressor.predict(x_pred)
        df_predict.loc[:, 'predict'] += y_pred/len(regressors)
    df_predict.loc[:, 'predict'] = df_predict['predict'].astype(int)

    return df_predict

if __name__ == "__main__":
    df2 = pd.read_csv("test_out.csv")
    with open('pickle.regressor', 'rb') as f:
        regressor = pickle.load(f)
    with open('pickle.encoder', 'rb') as f2:
        onehot_encoder = pickle.load(f2)
    category_fields = ["anchor", "dst_anchor", "carrierName"]
    numeric_fields=["speed1","relative_direction", "relative_distance", "relative_x", "relative_y",
     "abs_x", "abs_y", "sum_distance",  ]

    df2 = run_predict(df2, category_fields, numeric_fields,onehot_encoder, regressor)
    df2[['remain_seconds',"predict", "loadingOrder","timestamp"]].to_csv("predict.csv", index=False)
